﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.XmlHandle
{
    public class SearchBrigade
    {
        private string[] strIn;
        private List<ClassBrigade> listIn;
        private List<ClassBrigade> listBack = new List<ClassBrigade>();
        public SearchBrigade(List<ClassBrigade> cBrigade, string str)
        {
            listIn = cBrigade;

            if (Regex.IsMatch(str, @"^\s*$"))
            {
                listBack = cBrigade;
            }
            else
            {
                strIn = str.Split(' ');
                //System.Diagnostics.Debug.WriteLine("-------------------" + strIn.Length + "--------");
                //str = str.Replace(" ", ".*");
                if ((int)str[0] <= 127 && (int)str[0] >= 65)
                {
                    //strIn = @"^.*" + str.ToUpper() + ".*$";
                    this.sCode(strIn);
                }
                //Regex.IsMatch(str, @"^[\u4e00-\u9fa5]*.*$")
                else if ((int)str[0] >= 19968 && (int)str[0] <= 40879)
                {
                    this.sName(strIn);
                }
                else
                {
                    this.sAll(strIn);
                }
            }
        }

        private void sAll(string[] str)
        {
            List<ClassBrigade> tempI = listIn;
            List<ClassBrigade> tempO = new List<ClassBrigade>();
            foreach (string st in str)
            {
                if (!st.Equals(""))
                {
                    string s = st.ToUpper();
                    tempO.Clear();
                    foreach (ClassBrigade bri in tempI)
                    {
                        if (Regex.IsMatch(bri.Code, @"^.*" + s + ".*$"))
                        {
                            tempO.Add(bri);
                        }
                    }
                    foreach (ClassBrigade bri in tempI)
                    {
                        if (Regex.IsMatch(bri.FullName, @"^.*" + s + ".*$"))
                        {
                            tempO.Add(bri);
                        }
                    }
                    foreach (ClassBrigade bri in tempI)
                    {
                        if (Regex.IsMatch(bri.Postcode, @"^.*" + s + ".*$"))
                        {
                            tempO.Add(bri);
                        }
                    }
                    tempI.Clear();
                    tempI.AddRange(tempO);
                }
                else
                {
                    //tempO.Clear();
                    tempO.AddRange(tempI);
                    tempO = tempI;
                }
            }
            listBack = tempO;
            //throw new NotImplementedException();
        }

        public List<ClassBrigade> getListBack()
        {
            return listBack;
        }

        private void sCode(string[] str)
        {
            List<ClassBrigade> tempI = listIn;
            List<ClassBrigade> tempO = new List<ClassBrigade>();
            foreach (string st in str)
            {
                if (!st.Equals(""))
                {
                    string s = st.ToUpper();
                    tempO.Clear();
                    foreach (ClassBrigade bri in tempI)
                    {
                        //System.Diagnostics.Debug.WriteLine("------" + bri.Code + "--------" + s + "--------");
                        if (Regex.IsMatch(bri.Code, @"^.*" + s + ".*$"))
                        {
                            tempO.Add(bri);
                        }
                    }
                    tempI.Clear();
                    tempI.AddRange(tempO);
                }
                else
                {
                    tempO = tempI;
                }
            }
            listBack = tempO;
        }

        private void sName(string[] str)
        {
            List<ClassBrigade> tempI = listIn;
            List<ClassBrigade> tempO = new List<ClassBrigade>();
            foreach (string st in str)
            {
                if (!st.Equals(""))
                {
                    string s = st.ToUpper();
                    tempO.Clear();
                    foreach (ClassBrigade bri in tempI)
                    {
                        if (Regex.IsMatch(bri.FullName, @"^.*" + s + ".*$"))
                        {
                            tempO.Add(bri);
                        }
                    }
                    tempI.Clear();
                    tempI.AddRange(tempO);
                }
                else
                {
                    //tempO.AddRange(tempI);
                    tempO = tempI;
                }
            }
            listBack = tempO;
        }
    }
}
